﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BBMS.DLL.Forms.Base;
using BBMS.DLL.Base;
using BBMS.DLL.DataContract;
using BBMS.FrontEnd.Constant;
using System.Collections;
namespace BBMS.DLL.Forms.Catalog.Supplier
{
    public partial class frmSupplierUpdate : BBMS.DLL.Forms.Base.FrmEditBase
    {
        public frmSupplierUpdate(BBMS.DLL.Base.ObjectBase obj)
        {
            InitializeComponent();
            InitForm();
            curObj = obj;
        }

        private void InitForm()
        {
            chkQuayList.Items.Clear();
            cbxThanhPho.DataSource = FixedCode.CITY;
            if (ObjectLists.QuayList.GetKeys() != null)
            {
                foreach (object key in ObjectLists.QuayList.GetKeys())
                {
                    chkQuayList.Items.Add(key);
                }
            }
        }
        public new static readonly string FunctionKey = "DMNCC";
        protected override string GetFunctionKey() { return "DMNCC"; }


        protected override void OnLoad()
        {
            if (ObjectLists.LoaiNhaCungCapList.GetKeys() != null)
            {
                foreach (object key in ObjectLists.LoaiNhaCungCapList.GetKeys())
                {
                    cbxMaLoai.Items.Add(key);
                }
            }
            if (curObj != null)
            {
                NhaCungCap ncc = (NhaCungCap)curObj;
                txtMa.Text = ncc.Ma;
                txtTen.Text = ncc.Ten;
                txtTenVietTat.Text = ncc.TenVietTat;
                dteNgayMo.Value = ncc.NgayMo;
                cbxThanhPho.Text = ncc.ThanhPho;
                if(ncc.TinhTrang == TinhTrangNhaCungCap.DangHoatDong)
                    rbtnDangHoatDong.Checked = true;
                else if(ncc.TinhTrang == TinhTrangNhaCungCap.ChoThanhLy)
                    rbtnChoThanhLy.Checked = true;
                if(ncc.TinhTrang == TinhTrangNhaCungCap.DaThanhLy)
                    rbtnDaThanhLy.Checked = true;
                if(ncc.TinhTrang == TinhTrangNhaCungCap.TamDungThanhToan)
                    rbtnTamDungTT.Checked = true;
                
                txtMSTVAT.Text = ncc.MSTVAT;
                txtSoTK.Text = ncc.SoTK;
                txtTenNganHang.Text = ncc.TenNganHang;

                txtNguoiDaiDien.Text = ncc.NguoiDaiDien;
                txtDiaChi.Text = ncc.DiaChi;
                txtEmail.Text = ncc.Email;
                txtSoDT.Text = ncc.SoDienThoai;
                txtFax.Text = ncc.Fax;
                txtMatHang.Text = ncc.MatHang;
                
                cbxPhuongThucTT.SelectedIndex = -1;
                if ((int)ncc.PhuongThucTT < cbxPhuongThucTT.Items.Count)
                    cbxPhuongThucTT.SelectedIndex = (int)ncc.PhuongThucTT;
                if (ncc.PhuongThucTT == PhuongThucThanhToan.TheoThoiDiem)
                    txtGiaTriTT.Text = ncc.ThoiDiemTT;
                else txtGiaTriTT.Text = ncc.ThoiHanTT.ToString();
                txtHanMuc.Text = CommonTools.Helper.MoneyString(ncc.HanMuc);
                for (int i = 0; i < cbxMaLoai.Items.Count; i++)
                {
                    if (cbxMaLoai.Items[i].ToString().Equals(ncc.LoaiNCC))
                    {
                        cbxMaLoai.SelectedIndex = i;
                        break;
                    }
                }
                string strQuays = ncc.QuayList;
                if(!strQuays.EndsWith(";")) strQuays += ";";
                if(!strQuays.StartsWith(";")) strQuays = ";"+strQuays;
                for (int j = 0; j < chkQuayList.Items.Count; j++)
                {
                    string str = chkQuayList.Items[j].ToString();
                    if (strQuays.IndexOf(";" + str + ";") >= 0)
                        chkQuayList.SetItemChecked(j, true);
                }

                txtTen.Focus();
                txtMa.ReadOnly = true;

                if (!Config.CurUser.HaveRole(Role.Edit, GetFunctionKey()))
                {
                    txtMa.ReadOnly = true;
                    txtTen.ReadOnly = true;
                    txtTenVietTat.ReadOnly = true;
                    dteNgayMo.Enabled = false;
                    cbxThanhPho.Enabled = false;
                
                    txtMSTVAT.ReadOnly = true;
                    txtSoTK.ReadOnly = true;
                    txtTenNganHang.ReadOnly = true;

                    txtNguoiDaiDien.ReadOnly = true;
                    txtDiaChi.ReadOnly = true;
                    txtEmail.ReadOnly = true;
                    txtSoDT.ReadOnly = true;
                    txtFax.ReadOnly = true;
                    txtMatHang.ReadOnly = true;
                
                    cbxPhuongThucTT.Enabled = false ;
                    txtGiaTriTT.ReadOnly = true;
                    txtHanMuc.ReadOnly = true;
                    cbxMaLoai.Enabled = false;

                    btnSaveNew.Enabled = false;
                    btnReset.Enabled = false;
                    btnSaveClose.Enabled = false;
                    chkQuayList.Enabled = false;
                }
            }
            else
            {
                txtMa.Focus();
            }
        }

        protected override ObjectBase GetEditedObject()
        {
            if (txtMa.Text.Trim().Length == 0)
            {
                MessageBox.Show("Hãy nhập mã Nhà cung cấp");
                txtMa.Focus();
                return null;
            }
            if (txtTen.Text.Trim().Length == 0)
            {
                MessageBox.Show("Hãy nhập tên nhà cung cấp.");
                txtTen.Focus();
                return null;
            }
            if (cbxPhuongThucTT.SelectedIndex < 0)
            {
                MessageBox.Show("Hãy chọn phương thức thanh toán.");
                cbxPhuongThucTT.Focus();
                return null;
            }
            PhuongThucThanhToan ptTT = (PhuongThucThanhToan)cbxPhuongThucTT.SelectedIndex;

            LoaiNhaCungCap loai = (LoaiNhaCungCap)ObjectLists.LoaiNhaCungCapList.Get(cbxMaLoai.Text.Trim());
            if (loai == null)
            {
                MessageBox.Show("Hãy chọn loại nhà cung cấp.");
                cbxMaLoai.Focus();
                return null;
            }
            int thoiHanTT = 0;
            string thoiDiemTT = "";
            if (ptTT == PhuongThucThanhToan.TheoThoiHan)
            {
                try
                {
                    thoiHanTT = int.Parse(txtGiaTriTT.Text.Trim());
                }
                catch{ thoiHanTT = 0;}
                if (thoiHanTT <= 0)
                {
                    MessageBox.Show("Giá trị phương pháp thanh toán không hợp lệ.");
                    txtGiaTriTT.Focus();
                    return null;
                }
            }else{
                thoiDiemTT = txtGiaTriTT.Text.Trim();
                if (!NhaCungCap.IsValidThoiDiemTT(thoiDiemTT))
                {
                    MessageBox.Show("Giá trị phương pháp thanh toán không hợp lệ.");
                    txtGiaTriTT.Focus();
                    return null;
                }
            }
            TinhTrangNhaCungCap status = TinhTrangNhaCungCap.DangHoatDong;
            if (rbtnChoThanhLy.Checked) status = TinhTrangNhaCungCap.ChoThanhLy;
            else if (rbtnDaThanhLy.Checked) status = TinhTrangNhaCungCap.DaThanhLy;
            else if (rbtnTamDungTT.Checked) status = TinhTrangNhaCungCap.TamDungThanhToan;

            string strQuays = "";
            for (int i = 0; i < chkQuayList.Items.Count; i++)
            {
                if (chkQuayList.GetItemChecked(i))
                    strQuays += chkQuayList.Items[i].ToString() + ";";
            }
            strQuays = ";" + strQuays;

            return new NhaCungCap(txtMa.Text.Trim().ToUpper(), txtTen.Text.Trim(),
                txtTenVietTat.Text.Trim(),              
                dteNgayMo.Value.Date,
                txtDiaChi.Text.Trim(),
                cbxThanhPho.Text,
                status,
                txtMSTVAT.Text.Trim(),txtSoTK.Text.Trim(),txtTenNganHang.Text.Trim(),txtNguoiDaiDien.Text.Trim(),
                txtEmail.Text.Trim(),txtSoDT.Text.Trim(),txtFax.Text.Trim(),txtMatHang.Text.Trim(),
                ptTT,thoiHanTT,thoiDiemTT,
                txtHanMuc.Value,loai.Ma,strQuays);
        }

        protected override void Reset()
        {
            txtMa.Text = "";
            txtMa.ReadOnly = false;
            txtTen.Text = "";            
            txtTen.ReadOnly = false;
            txtTenVietTat.Text = "";
            txtTenVietTat.ReadOnly = false;
            dteNgayMo.Value = DateTime.Today.Date;
            dteNgayMo.Enabled = true;
            cbxThanhPho.Enabled = true;
            cbxThanhPho.Text = ""; 
            cbxThanhPho.SelectedIndex = -1;
          //  chxThoiVu.Checked = false; 
           // chxThoiVu.Enabled = true;
            rbtnDangHoatDong.Checked = true;

            txtMSTVAT.Text = "";
            txtMSTVAT.ReadOnly = false;
            txtSoTK.Text = "";
            txtSoTK.ReadOnly = false;
            txtTenNganHang.Text = "";
            txtTenNganHang.ReadOnly = false;

            txtNguoiDaiDien.Text = "";
            txtDiaChi.Text = "";
            txtEmail.Text = "";
            txtSoDT.Text = "";
            txtFax.Text = "";
            txtMatHang.Text = "";

            txtNguoiDaiDien.ReadOnly = false;
            txtDiaChi.ReadOnly = false;
            txtEmail.ReadOnly = false;
            txtSoDT.ReadOnly = false;
            txtFax.ReadOnly = false;
            txtMatHang.ReadOnly = false;

            cbxPhuongThucTT.SelectedIndex = 0;
            cbxPhuongThucTT.Enabled = true;
            txtGiaTriTT.ReadOnly = false;
            txtGiaTriTT.Text = "";
            txtHanMuc.Text = "0";
            txtHanMuc.ReadOnly = false;
            cbxMaLoai.Enabled = true;
            for (int i = 0; i < chkQuayList.Items.Count; i++)
            {
                chkQuayList.SetItemChecked(i, false);
            }

            if (curObj != null) OnLoad();

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            OnClose();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            Reset();
        }

        private void btnSaveNew_Click(object sender, EventArgs e)
        {
            OnSaveAndNew();
        }

        private void btnSaveClose_Click(object sender, EventArgs e)
        {
            OnSaveAndClose();
        }

        private void cbxPhuongThucTT_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtGiaTriTT.Text = "5";
        }

        private void txtMa_Leave(object sender, EventArgs e)
        {
            string ma = txtMa.Text.Trim().ToUpper();
            if (curObj == null && ObjectLists.NhaCungCapList.Get(ma) != null)
            {
                MessageBox.Show("Đã tồn tại nhà cung cấp có mã trùng với mã bạn nhập");
                txtMa.Focus();
            }
        }
    }
}
